from django.db import models
from dvadmin.utils.models import CoreModel

class Material(CoreModel):
    """原材料"""
    name = models.CharField(max_length=100, verbose_name="原材料名称")
    code = models.CharField(max_length=50, unique=True, verbose_name="物料编码")
    specification = models.CharField(max_length=200, verbose_name="规格型号")
    unit = models.CharField(max_length=20, verbose_name="计量单位")
    current_stock = models.DecimalField(max_digits=10, decimal_places=2, default=0, verbose_name="当前库存")
    minimum_stock = models.DecimalField(max_digits=10, decimal_places=2, default=0, verbose_name="最低库存")
    maximum_stock = models.DecimalField(max_digits=10, decimal_places=2, default=0, verbose_name="最高库存")
    
    class Meta:
        db_table = "inventory_material"
        verbose_name = "原材料"
        verbose_name_plural = verbose_name
        ordering = ('-create_datetime',)

class StockRecord(CoreModel):
    """库存记录"""
    RECORD_TYPE_CHOICES = (
        ('in', '入库'),
        ('out', '出库'),
        ('check', '盘点'),
    )
    
    material = models.ForeignKey(Material, on_delete=models.CASCADE, verbose_name="原材料")
    record_type = models.CharField(max_length=10, choices=RECORD_TYPE_CHOICES, verbose_name="记录类型")
    quantity = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="数量")
    before_quantity = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, verbose_name="操作前库存")
    after_quantity = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, verbose_name="操作后库存")
    remark = models.TextField(null=True, blank=True, verbose_name="备注")
    
    class Meta:
        db_table = "inventory_stock_record"
        verbose_name = "库存记录"
        verbose_name_plural = verbose_name
        ordering = ('-create_datetime',)
